package projetobancodedados;

import java.sql.*;
import java.util.ArrayList;
/**
 */
public class AlunoControle {
    connectionFactory cnFact;
    Connection con;
    public AlunoControle()
    {
        cnFact = new connectionFactory();
        con = cnFact.getConnection();
    }
    public void addAluno(Aluno a){
        try{
            String doc_aluno = a.getID();
            String nome = a.getNome();
            String disc = a.getDisc();
            String doc_prof = a.getProfessor();
            String pesquisa = a.getPesquisa();
            
            String querry = "INSERT INTO aluno(doc_aluno, nome, linha_de_pesquisa, doc_professor) VALUES(?, ?, ?, ?)";
            PreparedStatement psa = con.prepareStatement(querry);
            psa.setString(1, doc_aluno);
            psa.setString(2, nome);
            psa.setString(3, pesquisa);
            psa.setString(4, doc_prof);
            psa.executeUpdate();
            
            String querryM = "INSERT INTO matricula(doc_aluno, nome, cod_disciplina, linha_de_pesquisa) VALUES(?, ?, ?, ?)";
            PreparedStatement psm = con.prepareStatement(querryM);
            psm.setString(1, doc_aluno);
            psm.setString(2, nome);
            psm.setString(3, disc);
            psm.setString(4, pesquisa);
            psm.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
    public ArrayList listaAlunos(){
       ArrayList<Aluno> alunos = new ArrayList();
       Aluno a;
       try{
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("select aluno.doc_aluno, aluno.nome, aluno.linha_de_pesquisa, professor.nome as prof_nome "
                    + "from aluno inner join professor on aluno.doc_professor = professor.doc_professor");
            while(rs.next()){
                a = new Aluno(rs.getString("doc_aluno"), rs.getString("nome"
                        + ""), rs.getString("linha_de_pesquisa"), rs.getString("prof_nome"));
                alunos.add(a);
            }
       }catch(Exception e){
           e.printStackTrace();
       }
       return alunos;
    }
    public void closeConnection()
    {
        try{
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
